package screens;
import log.Logger;
import net.rim.device.api.i18n.MissingResourceException;
import net.rim.device.api.i18n.ResourceBundle;
import net.rim.device.api.ui.TransitionContext;
import net.rim.device.api.ui.Ui;
import net.rim.device.api.ui.UiEngineInstance;
import net.rim.device.api.ui.component.Menu;
import net.rim.device.api.ui.container.MainScreen;
import strings.WebcampusResource;
import test.Menues;

public class TransitionScreen extends MainScreen implements WebcampusResource{

	// RESOURCES
	private static ResourceBundle resources = ResourceBundle.getBundle(BUNDLE_ID, BUNDLE_NAME);

	public TransitionScreen(int transitionType) {
		attachTransition(transitionType);
	}

	//second constructor for passing styles to MainScreen superclass 
	public TransitionScreen(int transitionType, long mainscreenStyle) {
		super(mainscreenStyle);
		attachTransition(transitionType);
	}

	private void attachTransition(int transitionType) {
		UiEngineInstance engine = Ui.getUiEngineInstance();
		TransitionContext pushAction = null;
		TransitionContext popAction = null;

		switch (transitionType) {
		case TransitionContext.TRANSITION_FADE:
			pushAction = new TransitionContext(TransitionContext.TRANSITION_FADE);
			popAction = new TransitionContext(TransitionContext.TRANSITION_FADE);

			pushAction.setIntAttribute(TransitionContext.ATTR_DURATION, 750);
			popAction.setIntAttribute(TransitionContext.ATTR_DURATION, 750);
			break;
		case TransitionContext.TRANSITION_NONE:
			pushAction = new TransitionContext(TransitionContext.TRANSITION_NONE);
			popAction = new TransitionContext(TransitionContext.TRANSITION_NONE);
			break;
		case TransitionContext.TRANSITION_SLIDE:
			pushAction = new TransitionContext(TransitionContext.TRANSITION_SLIDE);
			popAction = new TransitionContext(TransitionContext.TRANSITION_SLIDE);

			pushAction.setIntAttribute(TransitionContext.ATTR_DURATION, 500);
			pushAction.setIntAttribute(TransitionContext.ATTR_DIRECTION, TransitionContext.DIRECTION_LEFT);

			popAction.setIntAttribute(TransitionContext.ATTR_DURATION, 500);
			popAction.setIntAttribute(TransitionContext.ATTR_DIRECTION, TransitionContext.DIRECTION_RIGHT);
			break;
		case TransitionContext.TRANSITION_WIPE:
			pushAction = new TransitionContext(TransitionContext.TRANSITION_WIPE);
			popAction = new TransitionContext(TransitionContext.TRANSITION_WIPE);
			break;
		case TransitionContext.TRANSITION_ZOOM:
			pushAction = new TransitionContext(TransitionContext.TRANSITION_ZOOM);
			popAction = new TransitionContext(TransitionContext.TRANSITION_ZOOM);
			break;
		default:
			pushAction = new TransitionContext(TransitionContext.TRANSITION_NONE);
			popAction = new TransitionContext(TransitionContext.TRANSITION_NONE);
		}
		engine.setTransition(null, this, UiEngineInstance.TRIGGER_PUSH, pushAction);
		engine.setTransition(this, null, UiEngineInstance.TRIGGER_POP, popAction);
	}

	// etapa 3 : fix menu 
	protected void makeMenu(Menu menu, int instance){
		Logger.log("opening menu");
		try {
			menu.deleteAll();
			// Menu
			menu.add(Menues.getAboutMenuItem(resources.getString(MENU_ABOUT),200,2));
			//menu.add(Menues.getConfigMenuItem(resources.getString(MENU_CONFIG), 300, 3));
			menu.add(Menues.getLogoutMenuItem(resources.getString(MENU_LOGOUT), 400, 4));
			
			//TODO etapa 3: uncomment for etapa 3: remove log screen access
			menu.add(Menues.getLogMenuItem("LOG", 500, 5));

			//			if (Controller.getInstance().getPushStatus()){
			//				menu.add(Menues.getDeActivatePushMenuItem(resources.getString(MENU_DEACTIVATEPUSH),1,1));
			//			} else {
			//				menu.add(Menues.getActivatePushMenuItem(resources.getString(MENU_ACTIVATEPUSH),1,1));
			//			}

			// etapa 3. uncomment for etapa 3
			if (Controller.getInstance().getPushStatus()){
				menu.add(Menues.getDeActivatePushMenuItem("Desactivar push",1,1));
			} else {
				menu.add(Menues.getActivatePushMenuItem("Activar push",1,1));
			}
			menu.add(Menues.getNotificationsMenuItem("Notificaciones", 600, 6));
			// end comment

		} 	catch (MissingResourceException e){
			Logger.log("opening menu error: missing resource exception");
			if (Controller.getInstance().getPushStatus()){
				menu.add(Menues.getDeActivatePushMenuItem("Desactivar push",1,1));
			} else {
				menu.add(Menues.getActivatePushMenuItem("Activar push",1,1));
			}
		} catch (Exception e){
			Logger.log("opening menu error: unknown exception");
		}
		super.makeMenu(menu, instance);
		Logger.log("opened menu");
	}

}